import React from "react";
import {LdhFieldSet, LdhSearchItem} from "@biz/PageCompnents";
import {LdhFormItem, LdhFormItemTip, LdhInput, LdhObjListSelect, Message} from "@biz/Components";
import BaseDetailPage from "@biz/BaseDetailPage";
import {PublicAction} from "@/base/BaseAction";
import {createAction} from "../Action";
import Config from "../config";
import Store from "../Store";

const prefix = "station-detail";
export default class StationDetail extends BaseDetailPage {

    constructor(props) {
        super(props);
        this.action = createAction();
        this.config = Config(this.action);
        this.store = new Store(props, this.config, this.action);
        this.className = prefix;
    }

    onGetDetailCompleted(detail) {
        let {detailStatus} = this.state;
        this.setState({shopList:[{id:detail.shopId, name:detail.shopName}]})
        if(detailStatus=="edit"){//编辑时，获取门店对应设备列表
            this.action.getShopUnassociatedDeviceList(detail.shopId,(voList=[])=> {
                let deviceList =[...detail.deviceSimpleList, ...voList];

                this.setState({deviceList})
            });
        }else{
            this.setState({deviceList:detail.deviceSimpleList})
        }
    }

    componentDidMount() {
        let {detailStatus} = this.state;
        if(detailStatus=="create"){//创建时需要获取门店列表
            PublicAction.allShopList(({voList = []}) => {
                this.setState({shopList: voList})
            });
        }

        super.componentDidMount();
    }

    handleShopChange(shopId){
        this.action.getShopUnassociatedDeviceList(shopId,(voList=[])=>{
            this.setState({deviceList:voList})
            this.form().setFieldsValue({deviceIds:[]});
            if(voList.length==0){
                Message.warning("当前门店不存在未关联工位的设备");
            }
        })
    }


    renderDetail(isDetail) {
        let {loading, shopList=[], deviceList=[], detailStatus} = this.state;
        return <LdhFieldSet title="基本信息" btn={!isDetail} loading={loading}>


            <LdhFormItem label="门店名称" name="shopId" required>
                <LdhObjListSelect list={shopList} onChange={this.handleShopChange.bind(this)}  isDetail={isDetail||detailStatus=="edit"}/>
            </LdhFormItem>

            <LdhFormItem label="工位名称" name="name" required isDetail={isDetail}>
                <LdhInput maxLength={60} showCount/>
            </LdhFormItem>

            <LdhFormItem label="关联设备" name="deviceIds" >
                <LdhObjListSelect list={deviceList}  mode="multiple" isDetail={isDetail}/>
            </LdhFormItem>
            {isDetail||<LdhFormItemTip>下拉框只展示当前门店还未关联工位的设备列表</LdhFormItemTip>}

            


        </LdhFieldSet>

    }
}